﻿using System;

namespace BEGDC.CC.Web.Control
{
	/// <summary>
	/// Summary description for ExportDataGridToExcel.
	/// </summary>
	public class ExportDataGridToExcel
	{
		public ExportDataGridToExcel()
		{
			
		}

		/// <summary>
		/// Export datagrid to excel
		/// </summary>
		/// <param name="page">source web page</param>
		/// <param name="datagrid">source datagrid</param>
		/// <param name="filename">export file name</param>
		public static void Export(System.Web.UI.Page page, System.Web.UI.WebControls.DataGrid datagrid, string filename)
		{
			Export(page,datagrid,filename, string.Empty );
		}
		#region //<--joan 2007/8/20
		/// <summary>
		/// Export datagrid to excel with sum cost
		/// </summary>
		/// <param name="page"></param>
		/// <param name="datagrid"></param>
		/// <param name="filename"></param>
		/// <param name="sumText"></param>
		public static void ExportWithSum(System.Web.UI.Page page, System.Web.UI.WebControls.DataGrid datagrid, string filename,string sumText)
		{
			ExportWithSum(page,datagrid,filename, sumText, string.Empty );
		}	

		/// <summary>
		/// Export datagrid to excel with sum cost
		/// </summary>
		/// <param name="page"></param>
		/// <param name="datagrid"></param>
		/// <param name="filename"></param>
		/// <param name="title"></param>
		public static void ExportWithSum(System.Web.UI.Page page, System.Web.UI.WebControls.DataGrid datagrid, string filename, string sumText, string title)
		{
			foreach(System.Web.UI.WebControls.DataGridColumn col in datagrid.Columns)
			{
                if (col.HeaderText.Contains("Action") || col.HeaderText.Contains("Active"))
					col.Visible = false;
			}

			if (filename == string.Empty)
				filename = "ASBCCExport.xls";
			page.Response.Clear();
			page.Response.Buffer = true;
			// Set the content type to Excel.
			page.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); 
			// Remove the charset from the Content-Type header.
			page.Response.ContentEncoding = System.Text.Encoding.UTF8;
			//page.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>"); 

			page.Response.ContentType = "application/vnd.ms-excel";
			

			// Turn off the view state.
			page.EnableViewState = false;

			System.IO.StringWriter tw = new System.IO.StringWriter();
			System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

			ClearControls(datagrid);
			// Get the HTML for the control.
			datagrid.RenderControl(hw);
			// Write the HTML back to the browser.
		
			if (sumText!="" && sumText!=string.Empty)
				hw.Write(sumText);

            //write BOM
            page.Response.Write('\uFEFF');

			if (title != string.Empty)
				page.Response.Write(title);

			page.Response.Write(tw.ToString());
			// End the response.
			page.Response.End();

		}


		#endregion


		/// <summary>
		/// Export datagrid to excel
		/// </summary>
		/// <param name="page">source web page</param>
		/// <param name="datagrid">source datagrid</param>
		/// <param name="filename">export file name</param>
		public static void Export(System.Web.UI.Page page, System.Web.UI.WebControls.DataGrid datagrid, string filename, string title)
		{
			foreach(System.Web.UI.WebControls.DataGridColumn col in datagrid.Columns)
			{
				if (col.HeaderText.Contains("Action") || col.HeaderText.Contains("Active"))
					col.Visible = false;
			}

			if (filename == string.Empty)
				filename = "ASBCCExport.xls";
			page.Response.Clear();
			page.Response.Buffer = true;
			// Set the content type to Excel.
			page.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); 
			// Remove the charset from the Content-Type header.
			page.Response.ContentEncoding = System.Text.Encoding.UTF8;
			//page.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>"); 

			page.Response.ContentType = "application/vnd.ms-excel";
			

			// Turn off the view state.
			page.EnableViewState = false;

			System.IO.StringWriter tw = new System.IO.StringWriter();

            


			System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

			ClearControls(datagrid);
			// Get the HTML for the control.
            tw.Write("<html><body>");
			datagrid.RenderControl(hw);
            tw.Write("</body></html>");
			// Write the HTML back to the browser.
            //write BOM
            page.Response.Write('\uFEFF');

			if (title != string.Empty)
				page.Response.Write(title);

			page.Response.Write(tw.ToString());
			// End the response.
			page.Response.End();

		}


        public static void Export(System.Web.UI.Page page, System.Web.UI.WebControls.GridView gridview, string filename)
        {
            Export(page, gridview, filename, string.Empty);
        } 

        public static void Export(System.Web.UI.Page page, System.Web.UI.WebControls.GridView gridview, string filename, string title)
        {
            gridview.Visible = true;
            gridview.AllowPaging = false;
            gridview.AllowSorting = true;
            gridview.DataBind();
            foreach (System.Web.UI.WebControls.DataControlField col in gridview.Columns)
            {
                if (col.HeaderText.Contains("Action") || col.HeaderText.Contains("Active"))
                    col.Visible = false;
            }

            if (filename == string.Empty)
                filename = "ASBCCExport.xls";
            page.Response.Clear();
            page.Response.Buffer = true;
            // Set the content type to Excel.
            page.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
            // Remove the charset from the Content-Type header.
            page.Response.ContentEncoding = System.Text.Encoding.UTF8;
            //page.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>"); 

            page.Response.ContentType = "application/ms-excel";

            // Turn off the view state.
            page.EnableViewState = false;
            
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

            ClearControls(gridview);
            // Get the HTML for the control.
            tw.Write("<html><body>");
            gridview.RenderControl(hw);
            tw.Write("</body></html>");
            // Write the HTML back to the browser.
            //write BOM
            page.Response.Write('\uFEFF');

            if (title != string.Empty)
                page.Response.Write(title);

            page.Response.Write(tw.ToString());
            // End the response.
            page.Response.End();
        }



		private static void ClearControls(System.Web.UI.Control control)
		{
			for (int i=control.Controls.Count -1; i>=0; i--)
			{
				ClearControls(control.Controls[i]);
			}
			if (!(control is System.Web.UI.WebControls.TableCell))
			{
				if (control.GetType().GetProperty("SelectedItem") != null)
				{
					System.Web.UI.LiteralControl literal = new System.Web.UI.LiteralControl();
					try
					{
						control.Parent.Controls.Add(literal);

						literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
						control.Parent.Controls.Remove(control);
					}
					catch
					{
					}
					
				}
				else
					if (control.GetType().GetProperty("Text") != null)
				{
					System.Web.UI.LiteralControl literal = new System.Web.UI.LiteralControl();
					control.Parent.Controls.Add(literal);
					literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
					control.Parent.Controls.Remove(control);
				}
			}
			return;
		}

	}
}
